<%@page import="com.lowagie.text.Cell"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ include file="/classes/DBHelper.class.jsp"%>
<%@ include file="/classes/CommonHelper.class.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	pageContext.setAttribute("basePath", basePath);
%>

<%
	int curr = 1;
	if (!nullOrEmpty(getParameter(request, "curr"))) {
		try {
			curr = Integer.parseInt(getParameter(request, "curr"));
		} catch (Exception e) {
			curr = 1;
		}
	}
	//获取数据
	String question = getParameter(request, "question");// request.getParameter("question");
	String status = getParameter(request, "status");
	if (null == status) {
		status = "A";
	}
	String time1 = getParameter(request, "time1");
	String time2 = getParameter(request, "time2");
	String sql = "SELECT * FROM APP_QA t WHERE 1 = 1 ";
	List<Object> params = new ArrayList<Object>();
	if (!nullOrEmpty(question)) {
		pageContext.setAttribute("question", question);
		sql += " and ( t.question like ? or t.answer like ?)";
		params.add("%" + question + "%");
		params.add("%" + question + "%");
	}
	if (!nullOrEmpty(time1)) {
		sql += " and t.create_time >= to_date(?,'YYYY-MM-DD HH24:MI:SS') ";
		params.add(time1);
		pageContext.setAttribute("time1", time1);
	}
	if (!"ALL".equals(status)) {
		sql += " and t.status =? ";
		params.add(status);
		pageContext.setAttribute("status", status);
	}
	if (!nullOrEmpty(time2)) {
		sql += " and t.create_time <= to_date(?,'YYYY-MM-DD HH24:MI:SS') ";
		params.add(time2);
		pageContext.setAttribute("time2", time2);
	}
	sql += " order by t.CREATE_TIME desc";
	int total = queryForPageCount(sql, params);
	double pageSize = 10;
	int totalPage = (int) Math.ceil(total / pageSize);
	if (totalPage < curr) {
		curr = totalPage;
	}
	List<Map<String, Object>> list = queryForListPage(sql, params, curr, (int) pageSize);

	pageContext.setAttribute("curr", curr);
	pageContext.setAttribute("list", list);
	pageContext.setAttribute("total", total);
%>
<!DOCTYPE html>
<html>
<head>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>问答系统-后台管理</title>

<script type='text/javascript'
	src="${basePath }plugins/jquery/jquery-1.12.2.min.js"></script>
<link rel="stylesheet"
	href="${basePath }plugins/layui/v1.0.9/css/layui.css" media="all">
<style type="text/css">
.question, .answer {
	overflow: hidden;
	width: 350px;
	display: inline-block;
	text-overflow: ellipsis;
	white-space: nowrap;
}
</style>
</head>
<body>
	<fieldset class="layui-elem-field layui-field-title"
		style="margin: 5px auto 5px auto">
		<legend style="color: red">问答系统-后台管理</legend>
	</fieldset>
	<form class="layui-form" method="post">
		<input type="hidden" id="is_update" value="false"> <input
			type="hidden" id="r" value="<%=System.currentTimeMillis()%>">
		<div class="layui-form-item" style="margin-bottom: 0px;">
			<div class="layui-inline">
				<label class="layui-form-label">关键字</label>
				<div class="layui-input-block">
					<input type="text" name="question" value="${question }"
						autocomplete="off" placeholder="请输入检索关键字" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">状态</label>
				<div class="layui-input-block">
					<select name="status">
						<option value="ALL">--全部--</option>
						<option value="A"
							<c:if test="${status eq 'A' }">selected="selected"</c:if>>未答复</option>
						<option value="B"
							<c:if test="${status eq 'B' }">selected="selected"</c:if>>已答复</option>
						<option value="C"
							<c:if test="${status eq 'C' }">selected="selected"</c:if>>未通过</option>
					</select>
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">时间范围</label>
				<div class="layui-input-inline" style="width: 170px;">
					<input type="text" id="time1" name="time1" value="${time1 }"
						readonly="readonly" autocomplete="off" class="layui-input"
						onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'#F{$dp.$D(\'time2\')}'})"
						placeholder="请选择开始时间">
				</div>
				<div class="layui-form-mid">-</div>
				<div class="layui-input-inline" style="width: 170px;">
					<input type="text" id="time2" name="time2" value="${time2 }"
						readonly="readonly" autocomplete="off" class="layui-input"
						onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'time1\')}'})"
						placeholder="请选择结束时间">
				</div>
			</div>
			<div class="layui-inline">
				<button class="layui-btn layui-btn-primary" id="searchBtn">
					<i class="layui-icon">&#xe615;</i> 查询
				</button>
			</div>
		</div>
		<table class="layui-table">
			<colgroup>
				<col width="50">
				<col width="350">
				<col width="350">
				<col width="200">
				<col>
			</colgroup>
			<thead>
				<tr>
					<th><input type="checkbox" name="" lay-skin="primary"
						lay-filter="allChoose"></th>
					<th>问题</th>
					<th>答复</th>
					<th>提问时间</th>
					<th>状态</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${list }" var="qa">
					<tr>
						<td><input type="checkbox" name="" lay-skin="primary">
						</td>
						<td>
							<p class="question" title="${qa.question }">${qa.question }</p>
						</td>
						<td>
							<p class="answer" title="${qa.answer }">${qa.answer}</p>
						</td>
						<td><fmt:formatDate value="${qa.create_time }"
								pattern="yyyy-MM-dd HH:mm:ss" /></td>
						<td><c:if test="${qa.status eq 'A' }">未答复</c:if> <c:if
								test="${qa.status eq 'B' }">已答复</c:if> <c:if
								test="${qa.status eq 'C' }">未通过</c:if></td>
						<td style="padding-bottom: 4px; padding-top: 6px;">
							<button type="button"
								class="layui-btn layui-btn-primary layui-btn-small"
								onclick="showDetail('${qa.qa_id }')">
								<i class="layui-icon">&#xe615;</i> 查看
							</button>
							<button type="button"
								class="layui-btn layui-btn-primary layui-btn-small"
								onclick="verify('${qa.qa_id }')">
								<i class="layui-icon">&#xe618;</i> 审核
							</button>
							<button type="button"
								class="layui-btn layui-btn-small layui-btn-danger"
								onclick="deleteQa('${qa.qa_id }')">
								<i class="layui-icon">&#xe640;</i> 删除
							</button>
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
		<input type="hidden" id="curr" name="curr" value="${curr }">
		<div id="layerPage"></div>
	</form>
	<script type="text/javascript"
		src="${basePath }plugins/layui/v1.0.9/layui.js"></script>
	<script charset="utf-8"
		src="${basePath }plugins/97date/4.8/WdatePicker.js"></script>
	<script type='text/javascript'>
		layui.use('form', function() {
			var $ = layui.jquery, form = layui.form();

			//全选
			form.on('checkbox(allChoose)', function(data) {
				var child = $(data.elem).parents('table').find(
						'tbody input[type="checkbox"]');
				child.each(function(index, item) {
					item.checked = data.elem.checked;
				});
				form.render('checkbox');
			});

		});

		function showDetail(qa_id) {
			layer.open({
				type : 2,
				title : '详细页面',
				shadeClose : true,
				shade : 0.5,
				area : [ '680px', '550px' ],
				content : '${basePath }qa_sys/sys/qa_detail.jsp?qa_id=' + qa_id
						+ '&r=' + Math.random()
			});
		}

		function verify(qa_id) {
			layer.open({
				type : 2,
				title : '审核页面',
				shadeClose : true,
				shade : 0.5,
				area : [ '680px', '550px' ],
				content : '${basePath }qa_sys/sys/qa_verify.jsp?qa_id=' + qa_id
						+ '&r=' + Math.random(),
				end : function() {
					if ($("#is_update").val() == "true") {
						$("#searchBtn").click();
					}
					return false;
				}
			});
		}
		layui.use([ 'laypage', 'layer' ], function() {
			var laypage = layui.laypage, layer = layui.layer;
			laypage({
				cont : 'layerPage',
				curr : '${curr }',
				pages : Math.ceil('${total}' / 10),
				skip : true,
				jump : function(obj, first) {
					//得到了当前页，用于向服务端请求对应数据
					if (!first) {
						var index = layer.load(0, {
							shade : false
						});
						$("#curr").val(obj.curr);
						$("#searchBtn").click();
					}
				}
			});
		});

		function deleteQa(qa_id) {
			layer.confirm('您确定要删除该信息，删除后无法恢复？', {
				btn : [ '确定并删除', '取消' ]
			//按钮
			}, function() {
				var index = layer.load(0, {
					shade : false
				});
				$.ajax({
					type : "GET",
					url : "${basePath }qa_sys/sys/qa_delete_do.jsp",
					data : {
						'qa_id' : qa_id
					},
					dataType : 'json',
					cache : false,//不缓存数据
					success : function(data) {
						layer.close(index);
						if (data.status == '200') {
							layer.alert(data.msg, {
								icon : 6
							}, function(index) {
								$("#searchBtn").click();
								layer.close(index);
							});
						} else {
							layer.alert(data.msg, {
								icon : 5
							})
						}
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						layer.close(index);
						layer.alert('连接错误', {
							icon : 5
						})
					}
				});
			});
		}
	</script>
</body>
</html>